<template>
  <div class="reg">
    <van-form @submit="onSubmit">
      <van-field
        class="username"
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="请输入手机号"
        label-width="50px"
        :rules="[
          { required: true, message: '用户名不能为空' },
          { pattern: checkUserName, message: '请输入正确内容' },
        ]"
      />
      <van-field
        class="psw"
        v-model="password1"
        type="password"
        name="密码"
        label="密码"
        placeholder="请输入至少六位数的密码"
        label-width="50px"
        :rules="[
          { required: true, message: '密码不能为空' },
          { pattern: checkpsw, message: '请输入正确内容' },
        ]"
      />
      <van-field
        class="psw"
        v-model="password2"
        type="password"
        name="密码"
        label="确认密码"
        placeholder="确认密码"
        label-width="60px"
        :rules="[
          { required: true, message: '密码不能为空' },
          { pattern: checkpsw, message: '请输入正确内容' },
          { validator, message: '两次密码必须一致' },
        ]"
      />
      <div>
        <van-button
          class="f-btn"
          round
          block
          type="info"
          native-type="submit"
          >注 册</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import { addUsers } from '@/api';
import { Toast } from 'vant';
export default {
  data() {
    return {
      username: '',
      password1: '',
      password2: '',
      checkUserName: /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
      checkpsw: /^\w{6,}$/,
    };
  },
  methods: {
    validator() {
      return this.password1 === this.password2;
    },
    async onSubmit() {
      const person = {
        user_name: '团粉111',
        user_phone: this.username,
        user_pwd: this.password1,
        user_sex: 1,
        user_headpic: 'https://img0.baidu.com/it/u=3310956763,1144195981&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
      };
      const res = await addUsers(person);
      const {
        data: { code, message },
      } = res;
      Toast(message);
      if (code == 0) {
        this.$router.push('/login');
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.reg {
  width: 84%;
  padding: 20px 8% 0 8%;
  .username {
    margin-bottom: 15px;
    border-radius: 3px;
  }
  .psw {
    margin-bottom: 15px;
    border-radius: 3px;
  }
  .f-btn {
    font-size: 15px;
    letter-spacing: 10px;
  }
}
</style>
